package lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by lx on 2017/5/14.
 * ReentrantlLock 瑞恩崔唠嗑
 * 需要手动释放锁
 *
 */
public class ReentrantlLockTest {

    Lock lock = new ReentrantLock();

    public void m(){
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"  m()  start");

            TimeUnit.SECONDS.sleep(2);
        }catch (Exception e){
            System.out.println("Exception " + e.getMessage());
        }
        finally {
            //手动释放锁
            lock.unlock();
        }
    }

    public static void main(String[] args) throws Exception{
        ReentrantlLockTest re = new ReentrantlLockTest();

        new Thread(re::m,"thread - 1").start();

        new Thread(re::m,"thread - 2").start();

    }
}
